﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace GFluentCode.Tools
{
    public enum EncodingChar
    {
        Hex,
        ASCII,
        UTF8
    }
    /// <summary>
    /// 字符串扩展
    /// </summary>
    public static class StringExtension
    {
        /// <summary>
        /// 使用系统默认字符集转byte数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] GToBytes(this string str)
        {
            return Encoding.Default.GetBytes(str);
        }

        /// <summary>
        /// 使用指定字符集转byte数组
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static byte[] GToBytes(this string str, Encoding encoding)
        {
            return encoding.GetBytes(str);
        }

        public static byte[] GToBytes(this string s, EncodingChar encodingChar)
        {
            byte[] result;
            switch (encodingChar)
            {
                case EncodingChar.Hex:
                    var tmp = s.Replace(" ", "");
                    if (tmp.Length % 2 != 0) return null;
                    result = new byte[tmp.Length / 2];
                    for (var i = 0; i < tmp.Length; i += 2)
                    {
                        result[i / 2] = Convert.ToByte(Convert.ToInt32(tmp.Substring(i, 2), 16));
                    }
                    break;
                case EncodingChar.ASCII:
                    result = Encoding.ASCII.GetBytes(s);
                    break;
                case EncodingChar.UTF8:
                    result = Encoding.UTF8.GetBytes(s);
                    break;
                default:
                    result = Encoding.Default.GetBytes(s);
                    break;
            }
            return result;
        }

        /// <summary>
        /// 字符串转时间
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime? GToDateTime(this string str)
        {
            DateTime dt;
            var b = DateTime.TryParse(str, out dt);
            return b ? dt : (DateTime?)null;
        }        

        /// <summary>
        /// 格式化
        /// </summary>
        /// <param name="str"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static string GToFormat(this string str,params string[] values)
        {
            return string.Format(str, values);
        }

        /// <summary>
        /// 转整型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Int32 GToInt32(this string s)
        {
            if (string.IsNullOrWhiteSpace(s))
                return 0;
            int d;
            return !int.TryParse(s, out d) ? 0 : d;
        }


        /// <summary>
        /// 整型字符串转十六进制串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="padLen"></param>
        /// <returns></returns>
        public static string GToHex(this string str,int padLen = 4)
        {
            return Convert.ToString(str.GToInt32(), 16).PadLeft(padLen, '0').ToUpper();
        }

        /// <summary>
        /// 路径转实际物理路径
        /// </summary>
        /// <param name="dir"></param>
        public static void GToDir(this string dir)
        {
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
        }
    }
}
